/**
  @creator: cilang
  @qq: 1019383856
  @bili: https://space.bilibili.com/433915419
  @gitee: https://gitee.com/OpencvLZG
  @github: https://github.com/OpencvLZG
  @since: 2023/11/29
  @desc: //
**/

package controller

import (
	"ginFlutterBolg/internal/model"
	"ginFlutterBolg/internal/service"
	. "ginFlutterBolg/internal/util"
	"github.com/gin-gonic/gin"
	"strconv"
)

type ArticleLikeController struct {
}

// LikeArticle 关注文章
func (a *ArticleLikeController) LikeArticle(c *gin.Context) {
	articleLike := new(model.ArticleLike)
	if err := c.Bind(&articleLike); err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}
	userController := new(UserController)
	user, err := userController.GetUserInfo(c)
	if err != nil {
		ResponseStatusOk(c, 400, "用户信息获取失败", err.Error())
		return
	}
	articleLike.UserId = user.Id
	articleLikeService := new(service.ArticleLikeSerivice)
	err = articleLikeService.LikeArticle(articleLike)
	if err != nil {
		ResponseStatusOk(c, 400, "数据插入失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "关注成功", "")

}

// GetArticleLikeList 获取关注的文章列表
func (a *ArticleLikeController) GetArticleLikeList(c *gin.Context) {
	page := c.Query("page")
	pageInt, err := strconv.Atoi(page)
	if err != nil {
		ResponseStatusOk(c, 400, "数据同步错误", err.Error())
		return
	}
	userController := new(UserController)
	user, err := userController.GetUserInfo(c)
	if err != nil {
		ResponseStatusOk(c, 400, "用户信息获取失败", err.Error())
		return
	}
	articleLikeService := new(service.ArticleLikeSerivice)
	articleLikeList, err := articleLikeService.GetArticleLikeList(pageInt, user.Id)
	if err != nil {
		ResponseStatusOk(c, 400, "文章获取失败", err.Error())
		return
	}
	ResponseStatusOk(c, 200, "获取成功", gin.H{
		"list": articleLikeList,
	})
}
